package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.union.huaben.UnionHuaBenMemberWeekData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_CROSS_UNION_HUA_BEN_OUT_TIME, desc = "本周话本成员出战信息")
public class CrossUnionHuaBenMemberOutTimeCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {


        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();

        UnionHuaBenProto.UnionHuaBenMemberOutTimeRespMsg.Builder respMsg = UnionHuaBenProto.UnionHuaBenMemberOutTimeRespMsg.newBuilder();
        ret = context.getRet();
        respMsg.setRet(ret);
        if(ret == 0){
            int weekId = CrossUnionHuaBenConfigMgr.getThisWeekId();
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(context.getUnionInfo().getUnionUid());
            if(memberMap != null){
                for (Map.Entry<Long, CrossUnionMember> memberEntry : memberMap.entrySet()) {
                    CrossUnionMember member = memberEntry.getValue();
                    UnionHuaBenMemberWeekData memberWeekData = CrossUnionHuaBenMgr.getUnionHuaBenMemberWeekData(weekId, member.getServerId(), member.getUserId());
                    if(memberWeekData == null){
                        continue;
                    }
                    UnionHuaBenProto.UnionHuaBenMemberOutTimeTemp.Builder builder = UnionHuaBenProto.UnionHuaBenMemberOutTimeTemp.newBuilder();
                    builder.setPlayerBase(PlayerBasePb.parsePlayerBaseTempMsg(member.getUserBaseInfo()));
                    builder.setTimes(memberWeekData.getSendTimes());
                    builder.setUserId(member.getUserId());
                    respMsg.addOut(builder);
                }
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_OUT_TIME, respMsg));
    }

    @Override
    public void sendData() {

    }
}
